export function IsPC () {
  if (navigator.userAgent.indexOf('Electron') !== -1) return 'PC'
  let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
  if (flag) {
    if (navigator.userAgent.indexOf('micromessenger') !== -1) return 'wechat'
    return 'mobile'
  }
  return 'web'
}

export function inArray (searchString, array) {
  for (var i = 0; i < array.length; i++) {
    if (searchString === array[i]) {
      return true
    }
  }
  return false
}
export function record2tr (record) {
  let data = []
  for (let i = 0; i < record.length; i++) {
    let item = record[i]
    data.push({ key: item.id.value, label: item.name.value })
  }
  return data
}
export function fields2tr (record) {
  // 字段表转穿梭框输入
  let data = []
  for (let it in record) {
    let item = record[it]
    data.push({ key: it, label: item.title, name: item.name })
  }
  return data
}
export function writeDict (fields) {
  let vals = {}
  for (let fieldName of fields) {
    let field = Object.values(fieldName)[0]
    if (field.name === 'id' || field.change) {
      if (
        [
          'display_name',
          'write_uid',
          'write_date',
          'create_date',
          '__last_update',
          'create_uid'
        ].indexOf(field.name) >= 0
      ) {
        continue
      }
      if (field.type === 'many2one') {
        console.debug('writeDict:', field)
        vals[field.name] = field.coid
      } else vals[field.name] = field.value
    }
  }
  return vals
}

// 下划线转换驼峰
export function toHump (name) {
  return name.replace(/\_(\w)/g, function (all, letter) {
    return letter.toUpperCase()
  })
}
// 驼峰转换下划线
export function toLine (name) {
  return name.replace(/([A-Z])/g, '_$1').toLowerCase()
}

export function modelToHump(name) {
  let name1 = 'all_' + name.replace(/(\.)/g, '_')
  return toHump(name1)
}
